package com.woniu.order.controller;


import com.woniu.common.dto.HBProductDto;
import com.woniu.common.dto.ShouhouPageDto;
import com.woniu.common.entity.ZyProduct;
import com.woniu.common.utils.GetCurrentUserId;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.PageParam;
import com.woniu.common.utils.ResponseData;
import com.woniu.order.dto.HBShouhouAndOrderDto;
import com.woniu.order.dto.ShouhouAndShouhouDetailAndOrderDto;
import com.woniu.order.dto.ShouhouDto;
import com.woniu.order.feign.EcssOrderFeign;
import com.woniu.order.feign.ProductFeign;
import com.woniu.order.pojo.*;
import com.woniu.order.service.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shouhou")
public class ShouhouController {
    @Autowired
    private ZyShouhouService shouhouService;
    @Autowired
    private ZyShouhoutypeService shouhoutypeService;
    @Autowired
    private ZyShouhoudetailService shouhoudetailService;
    @Autowired
    private ZyOrderService orderService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private EcssOrderFeign ecssOrderFeign;

    @PostMapping("/quxiaoShouhou")
    public ResponseData<?> quxiaoShouhou(@RequestBody ZyShouhou s) {
        //取消售后时，一个订单的所有商品都会取消售后
        String id = s.getId();//id为售后表id
        ZyShouhou shouhou = shouhouService.getById(id);
        String orderId = shouhou.getOid();
        ZyOrder order = orderService.getById(orderId);
        //订单状态改为已完成
        order.setState(4);
        ecssOrderFeign.quxiaoShouhouChangeOrderStatus(orderId);
        orderService.updateById(order);
        List<ZyShouhou> zyShouhouList = shouhouService.lambdaQuery()
                        .eq(ZyShouhou::getOid,orderId).list();

        for (ZyShouhou zyShouhou : zyShouhouList) {
            zyShouhou.setShStatus(2);
        }
        boolean b = shouhouService.updateBatchById(zyShouhouList);
        if (b) {
            return new ResponseData<>().success();
        } else {
            return new ResponseData<>().fail();
        }
    }

    @PostMapping("/fahuo")
    public ResponseData<?> fahuo(@RequestBody ZyShouhou s) {
        //id为售后表id
        //把该售后表的订单id所在的所有售后表都改为5
        //顾客点击已发货，售后状态改为5.待退款
        String id = s.getId();
        ZyShouhou shouhou = shouhouService.getById(id);
        String orderId = shouhou.getOid();
        List<ZyShouhou> shouhouList = shouhouService.lambdaQuery().eq(ZyShouhou::getOid, orderId).list();
        for (ZyShouhou zyShouhou : shouhouList) {
            if (zyShouhou.getShStatus()==3){
                zyShouhou.setShStatus(5);

            }
        }
        boolean b1 = shouhouService.updateBatchById(shouhouList);
        if (b1) {
            return new ResponseData<>().success();
        }else {
            return new ResponseData<>().fail();
        }

    }


    @PostMapping("/queryShouhou")
    public ResponseData<?> queryShouhou(@RequestBody PageParam<ShouhouDto> pageParam) {
        return shouhouService.queryShouhou(pageParam);
    }


    @SneakyThrows
    @PostMapping("/queryAllShouhouOrder")
    public ResponseData<?> queryAllShouhouOrder2() {
        ArrayList<ShouhouPageDto> shouhouPageDtoList = new ArrayList<>();
        List<ZyShouhou> zyShouhouList = shouhouService.list();
        for (ZyShouhou shouhou : zyShouhouList) {
            ShouhouPageDto shouhouPageDto = new ShouhouPageDto();
            String orderId = shouhou.getOid();
            ZyOrder order = orderService.getById(orderId);
            shouhouPageDto.setId(shouhou.getId());
            shouhouPageDto.setOrderId(orderId);
            shouhouPageDto.setActualAmount(order.getActualAmount());
            //
            String orderDetailId = shouhou.getShOdid();
            OrderDetail orderDetail = orderDetailService.getById(orderDetailId);
            String productId = orderDetail.getProductId();
            ZyProduct zyProduct = productFeign.selectProduct(productId).get(0);
            String productImg = zyProduct.getProductImg();

            shouhouPageDto.setProductName(orderDetail.getProductName());
            shouhouPageDto.setProductPrice(orderDetail.getProductPrice());
            shouhouPageDto.setBuyNum(orderDetail.getBuyNum());
            shouhouPageDto.setOrderAllmoney(shouhou.getOrderAllmoney());
            shouhouPageDto.setTuikuanMoney(shouhou.getTuikuanMoney());
            shouhouPageDto.setShReason(shouhouPageDto.getShReason());
            shouhouPageDto.setShShtype(shouhouPageDto.getShShtype());
            shouhouPageDto.setShStatus(shouhou.getShStatus());
            shouhouPageDto.setProductImg(productImg);
            shouhouPageDtoList.add(shouhouPageDto);
        }

        return new ResponseData<>().success(shouhouPageDtoList);

    }


    @PostMapping("/queryShouhouDetail")
    public ResponseData<?> queryShouhouDetail(@RequestBody String id) {
        String s = id.substring(0, id.length() - 1);
        ZyShouhou shouhou = shouhouService.getById(s);
        ZyShouhoudetail shouhoudetail = shouhoudetailService.lambdaQuery()
                .eq(ZyShouhoudetail::getShdShid, s).one();
        String orderId = shouhou.getOid();
        ZyOrder order = orderService.getById(orderId);
        ShouhouAndShouhouDetailAndOrderDto sdo = new ShouhouAndShouhouDetailAndOrderDto();
        sdo.setZyShouhou(shouhou);
        sdo.setZyShouhoudetail(shouhoudetail);
        sdo.setZyOrder(order);

        return new ResponseData<>().success(sdo);
    }

    @PostMapping("/updateShouhou")
    public ResponseData<?> updateShouhou(@RequestBody ZyShouhou shouhou) {
        boolean save = shouhouService.updateById(shouhou);
        if (save) {
            return new ResponseData<>().success();
        } else {
            return new ResponseData<>().fail();
        }
    }

    @PostMapping("/queryAllShouhouType")
    public ResponseData<?> queryAllShouhouType() {
        List<ZyShouhoutype> list = shouhoutypeService.list();
        return new ResponseData<>().success(list);
    }

    /**
     * 待发货状态的订单直接发起售后
     *
     * @param shouhou
     * @return
     */
    @PostMapping("/addShouhou")
    public ResponseData<?> addShouhou(@RequestBody ZyShouhou shouhou) {
        String orderId = shouhou.getId();

        return shouhouService.addShouhou(orderId);
    }

    /**
     * 订单完成（签收后）发起的售后
     *
     * @param shouhou
     * @return
     */
    @PostMapping("/addShouhouForYi")
    public ResponseData<?> addShouhouForYi(@RequestBody ZyShouhou shouhou) {
        return shouhouService.addShouhouForYiwancheng(shouhou);
    }

    @PostMapping("/tuikuan")
    public ResponseData<?> tuikuan(@RequestBody ZyShouhou zyShouhou) {
        //主要业务：给用户退款、用户消费金额减去、商品数量（质量问题不回滚）、
        // 售后list 5 状态都改成6

        //根据售后的id查出售后表
        //根据售后表中的订单id查出售后list
        //从售后list筛选出状态为待退款（5）
        String shouhouId = zyShouhou.getId();
        ZyShouhou shouhou = shouhouService.getById(shouhouId);
        String orderId = shouhou.getOid();
        List<ZyShouhou> shouhouList = shouhouService.lambdaQuery().eq(ZyShouhou::getOid, orderId).list();


        return shouhouService.tuikuan(shouhouList);
    }


    @PostMapping("/shenhe")
    public ResponseData<?> shenhe(@RequestBody ZyShouhou shouhou) {
        return shouhouService.shenhe(shouhou);
    }


    @PostMapping("/shenhenot")
    public ResponseData<?> shenhenot(@RequestBody ZyShouhou shouhou) {
        shouhou.setShStatus(2);
        boolean b = shouhouService.saveOrUpdate(shouhou);
        if (b) {
            return new ResponseData<>().success();
        } else {
            return new ResponseData<>().fail();
        }
    }


    @SneakyThrows
    @PostMapping("/getLoginUser")
    public ResponseData<?> getLoginUser() {
        String userId = GetCurrentUserId.getUserId();
        return new ResponseData<>().success(userId);


    }

    @GetMapping("/isGoingToShouhou")
    public ResponseData<?> isGoingToShouhou() {
        return shouhouService.isGoingToShouhou();
    }

    @PostMapping("/queryShouhouInKefu")
    public ResponseData<?> queryShouhouInKefu(@RequestBody PageParam<?> param) {
        return shouhouService.queryShouhouInKefu(param);
    }

}
